package com.gemantic.task.util;



import java.lang.reflect.Type;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.springframework.util.Assert;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;

/**
 * @author xiedi
 * @date 2022/09/16
 **/
public class JavaTypeFactory {
    private static final int JAVA_TYPE_MAPS_INIT_CAPACITY = 128;
    private static final ConcurrentMap<Type, JavaType> JAVA_TYPE_MAPS = new ConcurrentHashMap<>(JAVA_TYPE_MAPS_INIT_CAPACITY);

    private JavaTypeFactory() {
    }

    public static JavaType constructType(Type clazz) {
        Assert.notNull(clazz, "type 不能为null.");
        if (JAVA_TYPE_MAPS.containsKey(clazz)) {
            return JAVA_TYPE_MAPS.get(clazz);
        } else {
            JavaType javaType = TypeFactory.defaultInstance().constructType(clazz);
            JAVA_TYPE_MAPS.put(clazz, javaType);
            return javaType;
        }
    }
}
